/*https://leetcode.cn/problems/koko-eating-bananas/submissions/569225691/*/

class Solution {
public:
    int minEatingSpeed(vector<int>& piles, int h) {
        int n = piles.size();
        
        function<bool(int)>check = [&](int m){
            int time = 0;
            for(int i = 0;i < n;i++){
                if(piles[i] <= m)time++;
                else{
                    int t = piles[i]/m;
                    piles[i]%m == 0 ? time+=t : time = time+t+1;
                }
            }
            return time <= h;
        };
        int l = 1;//珂珂每个小时都至少吃掉一个香蕉 刚开始错误的认为每个小时至少吃掉一堆香蕉中最少的那堆 导致只通过了10个样例
        int r = *max_element(piles.begin(),piles.end());
        while(l < r){
            int mid = l + (r-l)/2;
            if(check(mid))
                r = mid;
            else
                l = mid+1;
        }
        return l;
    }
};